package com.cszy.yydqbfq.ui.activity

import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.cszy.yydqbfq.databinding.ActivityCloseFcmBinding

import com.cszy.yydqbfq.base.BaseVMActivity
import com.cszy.yydqbfq.common.AppConstants

import com.cszy.yydqbfq.ext.toast
import com.cszy.yydqbfq.utils.SpUtils
import com.cszy.yydqbfq.viewmodel.MainViewModel


class CloseFcmActivity : BaseVMActivity<MainViewModel, ActivityCloseFcmBinding>() {
    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initListener() {
      mBinding.headTitle.headBack.setOnClickListener {
          finish()
      }
        setupAutoFocus()
       //关闭防沉迷
        mBinding.btnClose.setOnClickListener {
            if (TextUtils.isEmpty(mBinding.edit1.text)|| TextUtils.isEmpty(mBinding.edit2.text)||
                TextUtils.isEmpty(mBinding.edit3.text)|| TextUtils.isEmpty(mBinding.edit4.text)
            ) {
                toast("请输入完整四位数密码")
                return@setOnClickListener
            }

            var currentPwd=getPhoneLast4Digits();//当前输入的密码
            var savePwd=SpUtils.getInstance().getString(AppConstants.SETTING_PWD)
            if (currentPwd.equals(savePwd)) {
                SpUtils.getInstance().putBoolean(AppConstants.ISOPEN_FCM,false)
                finish()
            }else{
                toast("抱歉，您输入的密码错误，请重新输入")
            }
        }
    }
    /**
     * 自动跳转输入
     */
    private fun setupAutoFocus() {
        val editTexts = arrayOf(mBinding.edit1, mBinding.edit2, mBinding.edit3, mBinding.edit4)
        editTexts.forEachIndexed { index, editText ->
            editText.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    if (s?.length == 1 && index < editTexts.lastIndex) {
                        editTexts[index + 1].requestFocus()
                    } else if (s?.length == 1 && index == editTexts.lastIndex) {
                        hideKeyboard()
                    }
                }
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            })
        }
    }

    // 隐藏键盘
    private fun hideKeyboard() {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(mBinding.edit4.windowToken, 0)
    }
    fun getPhoneLast4Digits(): String {
        return "${mBinding.edit1.text}${mBinding.edit2.text}${mBinding.edit3.text}${mBinding.edit4.text}"
    }
}